/* 
 * File:   Vector2D.cpp
 * Author: 75.02 - Grupo 7, 2do cuatrimestre 2012.
 * 
 * Created on November 2, 2012, 5:42 PM
 */

#include "Vector2D.h"
using namespace TP::Matematica;

Vector2D::Vector2D(){
    this->_x = 0;
    this->_y = 0;
}

Vector2D::Vector2D(long int pX, long int pY) {
    this->_x = pX;
    this->_y = pY;
}

Vector2D::~Vector2D() {
}

long int Vector2D::X(){
    return this->_x;
}

long int Vector2D::X(long int pX) {
    this->_x = pX;
    return pX;
}

long int Vector2D::Y(){
    return this->_y;
}

long int Vector2D::Y(long int pY){
    this->_y = pY;
    return pY;
}

string Vector2D::aXML(){

    TiXmlDocument* doc = new TiXmlDocument();
    TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
    doc->LinkEndChild( decl );
    
    doc->LinkEndChild(this->aElementoXML());

    TiXmlPrinter printer;
    doc->Accept(&printer);
    return printer.CStr();    
}

string Vector2D::aXML(const string& pPath){

    TiXmlDocument* doc = new TiXmlDocument(pPath.c_str());
    TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
    doc->LinkEndChild( decl );
    
    doc->LinkEndChild(this->aElementoXML());
    
    TiXmlPrinter printer;
    doc->Accept(&printer);
    return printer.CStr();    
}

void Vector2D::parseXML (const string& pDescriptor){

    TiXmlDocument doc;
    doc.Parse(pDescriptor.c_str(), 0, TIXML_ENCODING_UTF8);
    this->deElementoXML(doc.FirstChildElement());
}

TiXmlElement* Vector2D::aElementoXML (){
    
    TiXmlElement * element = new TiXmlElement("Vector2D");
    
    element->SetAttribute("x", this->_x);
    element->SetAttribute("y", this->_y);
    
    return element;
}

void Vector2D::deElementoXML (TiXmlElement* pElemento){

    if (strcmp (pElemento->Value(), "Vector2D") == 0)
    {
        this->_x = atoi(pElemento->Attribute("x"));
        this->_y = atoi(pElemento->Attribute("y"));
    }
}